package com.pai4j.pgc.service.community;
import com.pai4j.common.enums.community.CTCMenuEnum;
import com.pai4j.common.enums.community.ResourceBehaviorTypeEnum;
import com.pai4j.common.enums.community.ResourceTypeEnum;
import com.pai4j.domain.community.vo.response.cc.CTCHomeDataVO;
import com.pai4j.domain.community.vo.response.cc.CTCPathDetailVO;
import com.pai4j.pgc.service.behavior.AbstractResourceBehaviorService;
import com.pai4j.pgc.service.behavior.ResourceBehaviorFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

/**
 *  技术社区导航首页
 *
 * @author: CYM-pai
 * @date: 2025/10/27 13:56
 **/
@Service
public class CCPathHomeService {

    @Autowired
    private CCPathService pathService;
    @Autowired
    private CTCScheduleService ctcScheduleService;

    public CTCHomeDataVO getHomeDataFromCache(Long pathId, String userId) {
        CTCHomeDataVO homeData = new CTCHomeDataVO();
        /**
         * 查询栏目信息
         */
        CTCPathDetailVO pathDetail = pathService.getCTCPathDetailVOFromCache(pathId);
        homeData.setPath(pathDetail);
        homeData.setMenus(CTCMenuEnum.getMenus(pathDetail.getMenuCodes()));
        homeData.setBanners(ctcScheduleService.getHomeBannerFromCache(pathId));
        homeData.setResources(ctcScheduleService.getHomeResourceFromCache(pathId));
        homeData.setQuestions(ctcScheduleService.getHomeQuestionFromCache(pathId));
        homeData.setExperiences(ctcScheduleService.getHomeExperienceFromCache(pathId));
        homeData.setOpenSources(ctcScheduleService.getHomeOpenSourceFromCache(pathId));
        homeData.setNotices(ctcScheduleService.getCtcNoticeFromCache());
        homeData.setNavs(ctcScheduleService.getHomeNavListFromCache(pathId));
        homeData.setVideos(ctcScheduleService.getHomeVideoFromCache(pathId, 8));
        AbstractResourceBehaviorService starService =
                ResourceBehaviorFactory.getBehaviorService(ResourceBehaviorTypeEnum.STAR.getBehavior());
        Map<Long, Long> starMap = starService.getBehaviorCountFromCache(ResourceTypeEnum.CTC_PATH.getType(), Arrays.asList(28L));
        pathDetail.setStarCount(starMap.getOrDefault(28L, 0L));
        AbstractResourceBehaviorService attendService =
                ResourceBehaviorFactory.getBehaviorService(ResourceBehaviorTypeEnum.FOCUS.getBehavior());
        Map<Long, Long> attendCountMap = attendService.getBehaviorCountFromCache(ResourceTypeEnum.CTC_PATH.getType(), Arrays.asList(pathId));
        pathDetail.setAttendCount(attendCountMap.getOrDefault(pathId, 0L));
        if (StringUtils.isNotBlank(userId)) {
            Map<Long, Boolean> isStarMap = starService.getIsBehaviorFromCache(ResourceTypeEnum.CTC_PATH.getType(), Arrays.asList(pathId), userId);
            pathDetail.setIsStar(isStarMap.getOrDefault(pathId, false));
            Map<Long, Boolean> isAttendMap = attendService.getIsBehaviorFromCache(ResourceTypeEnum.CTC_PATH.getType(), Arrays.asList(28L), userId);
            pathDetail.setIsAttend(isAttendMap.getOrDefault(28L, false));
        }
        return homeData;
    }

    public boolean isAttend(String userId) {
        AbstractResourceBehaviorService attendService =
                ResourceBehaviorFactory.getBehaviorService(ResourceBehaviorTypeEnum.FOCUS.getBehavior());
        Map<Long, Boolean> isAttendMap = attendService.getIsBehaviorFromCache(ResourceTypeEnum.CTC_PATH.getType(), Arrays.asList(28L), userId);
        return isAttendMap.getOrDefault(28L, false);
    }
}
